<?php

class Util {

	private static $isDebugging = true;
	private static $usingAlert = false;

	public function __construct() {
		echo "Construindo Util";
	}

	public static function debug($message) {
		if( self::$isDebugging ) {
			if( self::$usingAlert ) {
				self::alert("Debug Message on " . date("l dS \of F Y h:i:s A") . "\\n-----------------------------------------------\\n$message");
			} else {
				echo "Debug Message on " . date("l dS \of F Y h:i:s A") . " : $message <br />";
			}
		}
	}
	
	public static function alert($message) {
		echo "<script>alert(\"$message\");</script>";
	}

	public static function debugPage($isEnabled, $message) {
		if($isEnabled) {
			self::debug($message);
		}
	}

	public static function invalidateSession() {
		@session_start();
		@session_destroy();
		header("Location: " . "login.php?erro=Logout efetuado com sucesso!");
	}

	public static function redirect($url) {
		header("Location: " . $url);
	}

	public static function validateRequest() {
		$var = Scope::loadVar("erro");
		if( $var != null ) {
			echo $var;
		}
	}

	public static function callSetter($obj, $attribute, $value) {
		$setterMethod = "set" . ucfirst($attribute);
		$obj->$setterMethod($value);
	}

	public static function callGetter($obj, $attribute) {
		$getterMethod = "get" . ucfirst($attribute);
		return $obj->$getterMethod();
	}
	
	public static function fillObject($class_name){
		$class = new ReflectionClass($class_name);
		$methods = $class->getMethods(ReflectionMethod::IS_PUBLIC);
		$setters = array();

		foreach( $methods as $method ) {
			if( strstr($method->getName(), 'set')) {
				$setters[] = $method;
			}
		}
		
		$obj = new $class_name();
		
		for($ind=0;$ind<sizeof($setters); $ind++) {
			$att = self::lcfirst(substr($setters[$ind]->getName(),3));
			$attValue = Scope::loadVar($att);
			Logger::debug('Util',"Setando o atributo " . $att . ' com o valor ' . $attValue);
			$setters[$ind]->invokeArgs($obj, array($attValue));
		}
		return $obj;
	}
	
	public static function isValid($obj) {
		return $obj != null && $obj != "";
	}
	
	public static function suggest($q){
	    $q = urlencode($q);
	    $identKey = "class=spell";
	    $identKeyTwo = "</i></b></a>";
	    $identKeyEnd = "class=spell><b><i>";
	    $content = file_get_contents("http://www.google.com/search?hl=pt-BR&q=.$q.");
	    $content = strstr($content, $identKey);
	    $end = strpos($content, $identKeyTwo);    
	    $content = substr($content, 0, $end);
	    $start = strpos($content, $identKeyEnd);
	    $content = substr($content, $start + strlen($identKeyEnd), strlen($content));
	    return $content;
	}
	
	public static function remove_from_array($array,$value){
	    $holding = array();
	  
	    foreach($array as $k => $v){
	        if ( is_array($v) ) {
	            $holding [$k] = remove_from_array ($v, $value);
	        } else if ($value != $v) {
	            $holding[$k] = $v; 
	        }
	    }
	    return $holding;  
	}

	public static function get_class($obj) {
		if( is_string( $obj ) ) {
			return $obj;
		} else {
			return get_class($obj);
		}
	}
	
	public static function includeFile($file, $isCentered) {
		if($isCentered) {
			echo "<center>\n";
			include $file;
			echo "</center>\n";
		} else {
			include $file;
		}
	}
	
	public static function getServerUrl() {
		$server = $_SERVER['SERVER_NAME'] . "/" . Messages::getConfig("appconfig.app.dir.name"); 
		$protocol = strpos($_SERVER['SERVER_PROTOCOL'], "HTTPS") > 0 ? "https://" : "http://"; 
		return $protocol . $server;
	}
	
	public static function getLink($value, $print = false) {
		$link = "";
		if( substr($value, 0,1) == "/" ) {
			$link = Config::getApplicationContext() . substr($value, 1, strlen($value));
		} else {
			$link = Config::getApplicationContext() . $value;	
		}
		if( $print ) {
			echo $link;
		} else {
			return $link;
		}		
	}
	
	public static function objectValidate($param) {
		return $param != null;
	}
	
	public static function stringValidate($param) {
		return self::objectValidate($param) && is_string($param) && $param != "";
	}
	
	public static function numberValidate($param) {
		return self::objectValidate($param) && is_int($param);
	}
}
?>